import * as PIXI from 'pixi.js'
import { PixiComponent } from '@inlet/react-pixi'

export default PixiComponent('Dynamic_Sprite', {
  create: (props) => {
    const { image } = props
    const texture = PIXI.Texture.fromImage(image)
    texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST
    console.log('Texture.fromImage', image)
    return new PIXI.Sprite(texture)
  },
  didMount: () => {},
  willUnmount: () => {},
  applyProps: (instance, oldProps, newProps) => {
    const { image, scale, anchor } = newProps
    instance.scale.set(scale)
    instance.anchor.set(anchor[0], anchor[1])
    if (oldProps.image !== image) {
      const texture = PIXI.Texture.fromImage(image)
      texture.baseTexture.scaleMode = PIXI.SCALE_MODES.NEAREST
      instance.setTexture(texture)
    }
  },
})
